package org.lex.dbq.view.driver;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.swt.events.SelectionEvent;
import org.lex.dbq.util.DriverLoader;
import org.lex.lang.AggregateException;

public class ListDriversHandler extends DriverActionHandler {
	protected ListDriversHandler(DriverDialog dialog) {
		super(dialog);
	}

	@Override
	public void widgetSelected(SelectionEvent e) {
		List<String> list = new ArrayList<String>();

		try {
			list = new DriverLoader().loadDriverClassNames(dialog.driver.getClasspath().getLocations());
		} catch (AggregateException ioe) {
			List<Exception> errors = ioe.getExceptions();
			log.warn(errors.size() + " exceptions found:");
			for (Exception er : errors) {
				log.warn("", er);
			}
		}
		log.info("Drivers: " + list);
		Collections.sort(list);
		dialog.className.removeAll();
		for (String s : list) {
			dialog.className.add(s);
		}
		if (false == list.isEmpty()) {
			dialog.className.select(0);
			dialog.selectDriver(list.get(0));
		}
	}
}
